package com.example.moudlec.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.moudlec.dto.RegistraDto;

import com.example.moudlec.dto.S_gather_Dto;
import com.example.moudlec.entity.Registration;

import com.example.moudlec.entity.RegistrationDet;
import com.example.moudlec.entity.S_gather;
import com.example.moudlec.entity.S_gather_details;
import com.example.moudlec.mapper.RegistrationDetMapper;
import com.example.moudlec.mapper.RegistrationMapper;
import com.example.moudlec.service.RegistrationService;
import com.example.moudlec.service.S_gatherSercice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@SuppressWarnings("all")
@Service
public class RegistrationServiceImpl implements RegistrationService {
    @Autowired
    RegistrationMapper regMapper;
    @Autowired
    RegistrationDetMapper regDetMapper;

    @Autowired
    S_gatherSercice sercice;

    @Override
    public void addRegstra(RegistraDto dto) {
        List<RegistrationDet> registrationDet = dto.getRegistrationDet();
        Registration registration = dto.getRegistration();
        registration.setRegId(UUID.randomUUID().toString());
        regMapper.addRegstra(registration);
        Long praentId = registration.getId();
        registrationDet.forEach(i -> {
            i.setParentId(praentId);
            regDetMapper.addRegstraDt(i);
        });
    }

    @Override
    public IPage<Registration> checkReglis(Long pageNum, Long pageSize) {
        Page<Registration> page = new Page<>(pageNum, pageSize, true);
        return regMapper.checkReglis(page);
    }

    @Override
    public void chkUpReg(RegistraDto registraDto) {
        Registration reg = registraDto.getRegistration();
        List<RegistrationDet> regDt = registraDto.getRegistrationDet();

        regMapper.chkUpReg(reg);
        regDt.forEach(i -> {
            regDetMapper.chkUpRegDt(i);
        });

        S_gather_Dto dto = new S_gather_Dto();

        String regIdr= reg.getRegId();
        String registerr = reg.getRegister();
        String reasonexactr = reg.getReasonexact();
        Integer amountSumr = reg.getAmountSum();
        Float costPriceSumr = reg.getCostPriceSum();
        String remarkr = reg.getRemark();
        String registerTimer = reg.getRegisterTime();
        String checkerr = reg.getChecker();
        String checkTimer = reg.getCheckTime();
        S_gather s_gather = new S_gather(regIdr,registerr , reasonexactr,
                amountSumr, costPriceSumr,remarkr , registerr, registerTimer,checkerr, checkTimer);
        List<S_gather_details> s_gather_details = new ArrayList<>();
        for (int i = 0; i < regDt.size(); i++) {
            Long parentId = s_gather.getId();
            Integer productId = regDt.get(i).getProductId();
            String productName = regDt.get(i).getProductName();
            Integer amount = regDt.get(i).getAmount();
            String amountUnit = regDt.get(i).getAmountUnit();
            Float costPrice = regDt.get(i).getCostPrice();
            Float subtotal = regDt.get(i).getSubtotal();
            Long orderId = regDt.get(i).getOrderId();
            Long exeorderdtId = regDt.get(i).getExeorderdtId();
            S_gather_details s = new S_gather_details(productId, productName, amount, amountUnit, costPrice, subtotal, orderId,exeorderdtId);

            s_gather_details.add(s);
        }
        dto.setS_gather(s_gather);
        dto.setS_gather_details(s_gather_details);
        sercice.insertS_gather2(dto);
    }
}
